import cv2
import numpy as np

def main():
    video_path = 'shaoshuai.mp4'  # 替换为实际视频路径
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        print("Error: Could not open video.")
        return

    fps = cap.get(cv2.CAP_PROP_FPS)
    delay = int(1000 / fps)

    # 定义一些可能用于模拟效果的参数
    edge_threshold1 = 150
    edge_threshold2 = 200
    dilation_iterations = 1
    erosion_iterations = 1
    blur_kernel_size = (5, 5)
    blur_sigma = 1.0

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        # 转换为灰度图
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # 应用高斯模糊
        blurred_frame = cv2.GaussianBlur(gray_frame, blur_kernel_size, blur_sigma)

        # 应用Canny边缘检测
        edges = cv2.Canny(blurred_frame, edge_threshold1, edge_threshold2)

        # 膨胀操作，使边缘变粗
        kernel_dilation = np.ones((3, 3), np.uint8)
        dilated_edges = cv2.dilate(edges, kernel_dilation, iterations=dilation_iterations)

        # 腐蚀操作，微调边缘
        kernel_erosion = np.ones((3, 3), np.uint8)
        final_edges = cv2.erode(dilated_edges, kernel_erosion, iterations=erosion_iterations)

        # 获取原视频的宽度和高度
        original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

        # 计算新的宽度和高度
        new_width = int(original_width * 2)
        new_height = int(original_height * 2)

        # 缩放图像
        resized_frame = cv2.resize(final_edges, (new_width, new_height))

        cv2.imshow('Video', resized_frame)
        if cv2.waitKey(delay) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()